<template>
	<view class="box">
		<!-- logo -->
		<view class="login_title">
			欢迎登录小U商城
		</view>
		<image class="login_logo"  src="../../static/logo/orange.png" alt="" />
		
		<button class="login_but" hover-class="touchOpacity" open-type="getPhoneNumber" @getphonenumber="wxlogin" >
			微信用户一键登录
		</button>
		<view  class="login_text" hover-class="touchOpacity" @click="toTextLoginPage">手机号验证注册/登录</view>
	</view>
</template>

<script>

	export default {
		data() {
			return {
			}
		},
		onLoad() {},
		methods: {
			toTextLoginPage(){
				uni.navigateTo({
					url:"../textlogin/textlogin"
				})
			},
			// 微信一键登录
			wxlogin(e){
				// #ifdef MP-WEIXIN
					let  _this = this; 
					// 执行登陆
					wx.login({
						async success(res){
							// res.code
							// console.log(res.code,e)
							let code =  res.code;
							let { encryptedData,iv } = e.detail;
							var option = {
								url:"wxlogin",
								data:{
									code,encryptedData,iv
								},
								method:"post"
							}
							let result =  await _this.$http(option);
							if(result.data.code ==200){
								// 登陆成功
								_this.$tips.showtoast(result.data.msg); //  提示时间默认是1.5s
								// 将登陆之后的获取到的数据存入缓存
								uni.setStorageSync("userInfo",result.data.list);
								// 更新app.vue中取全局变量的值
								let  app =  getApp();
								let {avatarurl,nickname,phone,uid} = result.data.list
								app.globalData.userInfo = {avatarurl,nickname,phone,uid};
								app.globalData.loginStatus = true; //登录
								app.globalData.header.authorization = result.data.list.token;
								setTimeout(()=>{
									// 返回上一层级
									uni.switchTab({
										url:"../user/user"
									})
								},1500)
								
							}
							
						}
					})
					
				// #endif
			}
		}
	}
</script>

<style>
	/* 导入外部的样式文件 */
	@import url("@/static/css/login.css");
</style>
